package com.starmark.gateway.pt.provider.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.starmark.core.framework.base.AbstractBaseService;
import com.starmark.core.framework.util.BeanMapper;
import com.starmark.core.framework.util.RedisUtils;
import com.starmark.gateway.pt.provider.entity.PtInterface;
import com.starmark.gateway.pt.provider.entity.PtInterfaceRequestField;
import com.starmark.gateway.pt.provider.mapper.PtInterfaceMapper;
import com.starmark.gateway.pt.provider.service.IPtInterfaceRequestFieldService;
import com.starmark.gateway.pt.provider.service.IPtInterfaceService;
import com.starmark.gateway.pt.provider.vo.PtInterfaceRequestFieldVo;
import com.starmark.gateway.pt.provider.vo.PtInterfaceVo;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 接口 服务实现类
 * </p>
 *
 * @author starmark
 * @since 2020-05-24
 */
@Service
public class PtInterfaceServiceImpl extends AbstractBaseService<PtInterfaceMapper, PtInterface> implements IPtInterfaceService {

    private static final String STARMARK_GATEWAY_INTERFACES = "starmark:gateway:interface:";

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private IPtInterfaceRequestFieldService fieldService;

    @Override
    public void buildPageSearch(Map<String, Object> params, QueryWrapper<PtInterface> eWrapper) {
        if (params.get("keyword") != null && StrUtil.isNotEmpty(params.get("keyword") + "")) {
            eWrapper.like("name", params.get("keyword") + "");
        }
        //查询工程ID
        eWrapper.eq("project_code", params.get("projectCode") + "");

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveOrUpdateInterface(PtInterfaceVo interfaceVo) {
        PtInterface ptInterface = BeanMapper.map(interfaceVo, PtInterface.class);
        this.saveOrUpdate(ptInterface);
        //先删除子数据
        fieldService.deleteItemsByInterfaceId(ptInterface.getId());
        List<PtInterfaceRequestFieldVo> fieldVos = interfaceVo.getRequestFields();
        fieldVos.forEach(fieldVo -> {
            PtInterfaceRequestField interfaceRequestField = BeanMapper.map(fieldVo, PtInterfaceRequestField.class);
            interfaceRequestField.setInterfaceId(ptInterface.getId());
            fieldService.save(interfaceRequestField);
        });
        redisUtils.set(STARMARK_GATEWAY_INTERFACES + ptInterface.getProjectCode() + ":" + ptInterface.getCode(), JSON.toJSONString(interfaceVo));
        return ptInterface.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteInterface(String interfaceId) {
        Assert.hasText(interfaceId, "接口ID不允许为空");
        PtInterface ptInterface = this.getById(interfaceId);
        Assert.notNull(ptInterface, "接口不存在:" + interfaceId);
        //先删除子数据
        fieldService.deleteItemsByInterfaceId(interfaceId);
        this.removeById(interfaceId);
        redisUtils.del(STARMARK_GATEWAY_INTERFACES + ptInterface.getProjectCode() + ":" + ptInterface.getCode());
    }

    @Override
    public PtInterfaceVo getPtInterfaceVo(String interfaceId) {
        Assert.hasText(interfaceId, "接口ID不允许为空");
        PtInterface ptInterface = this.getById(interfaceId);
        Assert.notNull(ptInterface, "接口不存在:" + interfaceId);
        PtInterfaceVo interfaceVo = BeanMapper.map(ptInterface, PtInterfaceVo.class);
        List<PtInterfaceRequestField> fields = fieldService.getInterfaceFieldList(interfaceId);
        interfaceVo.setRequestFields(BeanMapper.mapList(fields, PtInterfaceRequestFieldVo.class));
        return interfaceVo;
    }

    @Override
    public void overload() {
        List<PtInterface> ptInterfaces=this.list();
        for(PtInterface ptInterface:ptInterfaces){
            PtInterfaceVo ptInterfaceVo=this.getPtInterfaceVo(ptInterface.getId());
            redisUtils.set(STARMARK_GATEWAY_INTERFACES + ptInterface.getProjectCode() + ":" + ptInterface.getCode(), JSON.toJSONString(ptInterfaceVo));
        }
    }


}
